package com.neu.flashmall.product.controller;

import com.neu.common.utils.Result;
import com.neu.flashmall.product.entity.PmsAttr;
import com.neu.flashmall.product.service.PmsAttrService;
import com.neu.flashmall.product.vo.AttrFilterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/pms_attr")
@Api("属性Controller")
public class PmsAttrController {
    @Resource
    private PmsAttrService pmsAttrService;

    @GetMapping(value = "/getAllAttrByGroup")
    @ApiOperation("获取某个属性组的所有属性")
    public Result getAllAttrByGroup(Integer groupId){
        List<PmsAttr> result = pmsAttrService.getAllAttrByGroup(groupId);
        if(result != null){
            return Result.ok().put(result);
        }
        return Result.error();
    }

    @PutMapping(value = "/check/addAttr")
    @ApiOperation("添加属性")
    public Result addAttr(@RequestBody PmsAttr pmsAttr){
        String result = this.pmsAttrService.addAttr(pmsAttr);
        if ("添加成功".equals(result)){
            return Result.ok().put(pmsAttr.getId()).setMessage(result);
        }
        return Result.ok().setMessage(result);
    }

    @PostMapping(value = "/check/updateAttr")
    @ApiOperation("更新属性")
    public Result updateAttr(@RequestBody PmsAttr pmsAttr){
        String result = this.pmsAttrService.updateAttr(pmsAttr);
        if ("更新成功".equals(result)){
            return Result.ok().put(pmsAttr.getId()).setMessage(result);
        }
        return Result.ok().setMessage(result);
    }

    @DeleteMapping("/check/deleteAttr")
    @ApiOperation("删除属性")
    public Result deleteAttr(Long id){
        String result = this.pmsAttrService.deleteAttr(id);
        if("删除成功".equals(result)){
            return Result.ok().setMessage(result);
        }
        return Result.error().setMessage(result);
    }

    @GetMapping("/check/changeSearchType")
    public Result changeSearchType(Long id, Integer now){
        String outcome = this.pmsAttrService.changeSearchType(id,now);
        if("检索状态代码异常或者数据库异常".equals(outcome)){
            return Result.error().setMessage(outcome);
        }
        return Result.ok().setMessage(outcome);
    }

    @GetMapping("/check/changeValueType")
    public Result changeValueType(Long id,Integer now){
        String outcome = this.pmsAttrService.changeValueType(id,now);
        if("属性值状态代码异常或者数据库异常".equals(outcome)){
            return Result.error().setMessage(outcome);
        }
        return Result.ok().setMessage(outcome);
    }

    @GetMapping("/check/changeAttrType")
    public Result changeAttrType(Long id,Integer now){
        String outcome = this.pmsAttrService.changeAttrType(id,now);
        if("属性类型代码异常或者数据库异常".equals(outcome)){
            return Result.error().setMessage(outcome);
        }
        return Result.ok().setMessage(outcome);
    }

    @GetMapping("/check/changeEnable")
    public Result changeEnable(Long id,Integer now){
        String outcome = this.pmsAttrService.changeEnable(id,now);
        if("已经启用该属性".equals(outcome) || "已经禁用该属性".equals(outcome)){
            return Result.ok().setMessage(outcome);
        }
        return Result.error().setMessage(outcome);
    }

    @GetMapping("/check/changeShow")
    public Result changeShow(Long id,Integer now){
        String outcome = this.pmsAttrService.changeShow(id,now);
        if("展示状态代码异常或者数据库异常".equals(outcome)){
            return Result.error().setMessage(outcome);
        }
        return Result.ok().setMessage(outcome);
    }

    @GetMapping("/getAttrListByCategory")
    public Result getAttrListByCategory(Long categoryId){
        List<PmsAttr> result = this.pmsAttrService.getByCategoryId(categoryId);
        return Result.ok().put(result);
    }

    @GetMapping("/getAllSkuAttrByCategory")
    public Result getAllSkuAttrByCategory(Long categoryId){
        List<PmsAttr> result = this.pmsAttrService.getSkuAttrByCategoryId(categoryId);
        return Result.ok().put(result);
    }

    @GetMapping("/getAttrFilterVo")
    public Result getAttrFilterVo(Long categoryId){
        AttrFilterVo attrFilterVo = this.pmsAttrService.getAttrFilterVo(categoryId);
        return Result.ok().put(attrFilterVo);
    }
}
